package homework.Day0322;

import java.util.Arrays;

public class ArrayListText {

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        printArr(arr);
        printArr1(arr,3);
        copyArray(arr);

    }


    /**
     * 遍历打印数组
     */
    public static void printArr(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("[%d] = %d  ",i,arr[i]);
        }
        System.out.println();
    }

    public static void printArr1(int[] arr ,int numberOfLines){
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("[%d] = %d  ",i,arr[i]);
            if ((i + 1) % numberOfLines == 0){
                System.out.println();
            }
        }
        if (arr.length % numberOfLines != 0){
            System.out.println();
        }
    }

    /**
     * 复制数组
     */

    public static int[] copyArray(int[] arr){
        int[] copyArr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copyArr[i] = arr[i];
        }
        System.out.println(Arrays.toString(copyArr));
        return copyArr;
    }

    /**
     * 数组扩容
     * arr = Arrays.copyOf(arr,10);
     */

    public static int[] arrayExpansion(int[] arr, int length){
        if (length < 0){
            throw new  IllegalArgumentException("长度不合法");
        }
        int[] newArr = new int[length];
        for (int i = 0; i < Math.min(arr.length,length); i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }
}
